package edu.dublbo.app.habitplusfavorite.controller;

import edu.dublbo.app.habitplusfavorite.model.Resource;
import edu.dublbo.app.habitplusfavorite.model.ResourceCatalog;
import edu.dublbo.app.habitplusfavorite.service.IResourceCatalogService;
import edu.dublbo.app.habitplusfavorite.service.IResourceService;
import edu.dublbo.commons.lang.PageResult;
import edu.dublbo.commons.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/favorite")
public class FavoriteController {
    @Autowired
    private IResourceCatalogService resourceCatalogService;
    @Autowired
    private IResourceService resourceService;

    @GetMapping(value = "/resource-catalog-list")
    public Result listResourceCatalog (){
        List<ResourceCatalog> list = resourceCatalogService.listOfChildren();
        return Result.success(list);
    }

    @GetMapping(value = "/list")
    public PageResult list(int page, int limit) {
        log.info("page: {}, limit: {}", page, limit);
        return resourceService.listOfPage(page, limit);
    }

    @PostMapping
    public Result saveOrModify(@RequestBody Resource re) {
        log.info("传过来的对象为：{}", re);
        boolean b = resourceService.saveOrUpdate(re);
        return b ? Result.success() : Result.fail();
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable int id) {
        log.info("id={}", id);
        resourceService.delete(id);
        return Result.success();
    }
}
